/*
 * @Description: 流程分类 - 接口
 * @Author: DHL
 * @Date: 2022-08-19 15:43:19
 * @LastEditors: DHL
 * @LastEditTime: 2023-02-15 09:16:43
 */

import { $get, $post, $put, $exportExcel } from '@/utils/http'
import { ContentTypeEnum } from '@/constant/http'
import {
  FlowCategoryModel,
  FlowCategoryParams,
  FlowCategoryPageParams,
  FlowCategoryPageResultModel,
  FlowCategoryResultModel,
  FlowCategoryListResultModel
} from './model/flowCategoryModel'
import { ElMessage } from 'element-plus'
import { listToTree } from '@/utils/treeUtils'

/**
 * 分页查询
 */
export const apiFlowCategorySearch = (searchParams: FlowCategoryPageParams) =>
  $get<FlowCategoryPageResultModel>({
    url: '/flowCategory/search',
    params: searchParams
  })

/**
 * 根据ID获取详情
 */
export const apiFlowCategoryGetById = (id: string) =>
  $get<FlowCategoryResultModel>({
    url: '/flowCategory/getById',
    params: { id: id }
  })

/**
 * 保存信息
 */
export const apiFlowCategorySave = (saveParams: FlowCategoryModel) =>
  $post<FlowCategoryResultModel>({
    url: '/flowCategory/save',
    contentType: ContentTypeEnum.json,
    params: saveParams
  })

/**
 * 更新信息
 */
export const apiFlowCategoryUpdateById = (updateParams: FlowCategoryModel) =>
  $put<FlowCategoryResultModel>({
    url: '/flowCategory/updateById',
    contentType: ContentTypeEnum.json,
    params: updateParams
  })

/**
 * 批量删除信息
 */
export const apiFlowCategoryBatchDeleteByIds = (ids: string) =>
  $post<FlowCategoryResultModel>({
    url: '/flowCategory/batchDeleteByIds',
    params: { ids: ids }
  })

/**
 * 批量逻辑删除
 */
export const apiFlowCategoryLogicBatchDeleteByIds = (ids: string) =>
  $post<FlowCategoryResultModel>({
    url: '/flowCategory/logicBatchDeleteByIds',
    params: { ids: ids }
  })

/**
 * 导出
 */
export const apiFlowCategoryExport = (searchParams: FlowCategoryParams, fileName: string) =>
  $exportExcel<FlowCategoryResultModel>(
    {
      url: '/flowCategory/exportExcel',
      params: searchParams
    },
    fileName
  )

/**
 * 查询流程分类树
 * @param searchParams
 * @returns
 */
export const apiFlowCategorySearchTree = (searchParams: FlowCategoryParams) => {
  return new Promise<{ treeData: Array<FlowCategoryModel>; listData: Array<FlowCategoryModel> }>(
    (resolve, reject) => {
      $get<FlowCategoryListResultModel>({
        url: '/flowCategory/searchList',
        params: searchParams
      }).then((result) => {
        if (result?.success) {
          const treeData = listToTree(result.data, 'id', 'pid', '')
          resolve({
            treeData: treeData,
            listData: result.data
          })
        } else {
          ElMessage.error(result?.message)
          reject(result?.message)
        }
      })
    }
  )
}
